home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / mmailp.idb / usr / lib / Zmail / bin / lookup.ad.z / lookup.ad
Encoding:
Text File  |  1997-01-22  |  2.5 KB  |  79 lines

  1. :
  2. #
  3. # E-Mail address directory lookup script to read $HOME/.addresses
  4. #
  5. # The format of the $HOME/.addresses file is simple:
  6. #    Place one E-Mail address on each line.
  7. #    Descriptive comments that should be displayed but NOT included in
  8. #    the address headers may follow the address, enclosed in [ ].
  9. #
  10. # Samples:
  11. #    "Fred Jones" <fjones@jones.com>    [President of Jones Inc.]
  12. #    mary@ucla.edu (Mary Johnson)
  13. #    herbert (Herb Smith)        [Herb, down in Accounting]
  14. #
  15. # The environment variable ADDRESSES can be used to reference another file
  16. # instead of $HOME/.addresses, in the same format.
  17. #
  18. if test -z "$ADDRESSES"
  19. then
  20.     ADDRESSES=$HOME/.addresses
  21. fi
  22. if test $# -ne 2
  23. then
  24.     echo "`basename $0`: Expected 2 args, got $#"
  25.     exit 3                    # Incorrect arguments
  26. fi
  27. matches=${TMPDIR-/tmp}/admatch$$
  28.  
  29. # make SP = space + tab
  30. SP=`echo x | tr x '\011'``echo x | tr x '\040'`
  31.  
  32. # extract address part if this doesn't look like it's already a regex
  33. # this won't work absolutely 100% but it should get it right on
  34. # non-perverse cases
  35. addressonly=$2
  36. if echo "$addressonly" | grep -v '[]\*]' >/dev/null
  37. then
  38.     # the following sed expressions do this, in order:
  39.     # - assume that anything in double quotes is a comment and drop it
  40.     # - pull out the address from any remaining comments
  41.     # - squeeze adjacent whitespace, strip leading/trailing whitespace
  42.     # - drop anything that looks like a relay/host name; in particular
  43.     #   user@host, host!user@relay, user%host@relay, host1!host2!...!user
  44.     addressonly=`echo " $addressonly " \
  45.         | sed -e 's/\([^!]\)"[^"]*"\([^@]\)/\1\2/g' \
  46.               -e 's/([^)]*)//g' -e 's/^.*<\([^>]*\)>.*$/\1/' \
  47.               -e "s/[$SP][$SP]*/ /g" -e "s/^[$SP]//" -e "s/[$SP]$//" \
  48.               -e 's/@.*$//' -e 's/%.*$//' -e 's/^.*!\([^!]*\)$/\1/'`
  49.  
  50.     # if we somehow nuked the entire string, fall back on the original
  51.     # string, stripping one level of quoting if it exists
  52.     test -z "$addressonly" && addressonly=`echo "$2" \
  53.         | sed -e 's/^"\(.*\)"$/\1/'`
  54. fi
  55.  
  56. (egrep -i "$addressonly" $ADDRESSES || fgrep -i "$addressonly" $ADDRESSES) > $matches 2>/dev/null
  57. count=`wc -l < $matches`
  58. EXIT=1                        # Execution failure
  59. if test $count -eq 0
  60. then
  61.     echo "$2"
  62.     EXIT=4                    # No matches found
  63. elif test $count -gt $1 -a $1 -gt 0
  64. then
  65.     echo "Matched $count names (max $1)"
  66.     echo "Use a more specific pattern please."
  67.     EXIT=2                    # Too many matches
  68. else
  69.     cat $matches
  70.     if test $count -eq 1
  71.     then
  72.     EXIT=5                    # Exactly one match
  73.     else
  74.     EXIT=0                    # At least one match
  75.     fi
  76. fi
  77. rm -f $matches
  78. exit $EXIT
  79.